﻿$NameSpace("$lang.ClassLoader");

$ClassLoader=$class.create("$ClassLoader")//function(){this.initialize.apply(this, arguments);}
$ClassLoader.prototype={
		
	initialize : function(){
		this.loadedCSS=[];
		this.loadedJS=[];
		this.extNames=this.genExtNames();	},
	
	genExtNames : function(){		var extNames=[];		extNames["js"]=".js";		extNames["css"]=".css";		return extNames;	},		getExtName : function(type){		return this.extNames[type];	},		getContextPath : function(){
		var cPath = "/";
		if(typeof(appConfig)=="string"){
			cPath = appConig.getContextPath();
		}else if(typeof(requestContextPath)=="string"){
			cPath = requestContextPath;
		}else if(typeof(appContextPath)=="string"){
			cPath = appContextPath;
		}
		return this.renderPath(cPath);
	},
	
	/* 
	 * load('include.mian', 'js');
	 * load('style.xp;style.vista', 'css');
	 */
	load : function (names, type){
		var msg = "";//should use HashMap;
		try{
		  var arr = names.replace(/;/g,",").split(",");
		  for(var i=0;i<arr.length;i++){
		  	try{
		  		this.loadByName(arr[i],type);
		  	}catch(e){
		  		msg+=("\nloading "+arr[i]+" : "+e.description);
		  	}
		  }
		}catch(e){
	  		msg+=("\nloading : "+names+" : "+e.description);
		}
		if(msg){
			alert("$import(,) has some error: "+msg);
			throw new Error(0,msg);
		}
	},

	/** @inner	**/
	loadByName : function(iName,type){
		var QName = $objectFactory.renderName(iName);
		if(!QName)return;

		/* reflect invoke;
		var fn = eval("this.load"+type.toUpperCase());
		if(typeof(fn)=="function"){
			(fnBind(fn,this,QName))();
		}
		*/
		if (type == "css") {
			this.loadCSS(QName);
		} else if(type=="i18n") {
			this.loadI18N(QName);
		} else{
			this.loadJS(QName);
		}
	},

	/** @inner	**/
	loadCSS : function (QName) { 
		if( QName.length<1 || this.loadedCSS[QName] ) return false;
		var mUrl = QName.replace(/[\.]/g,"/")+this.getExtName("css");		var fUrl = this.getContextPath()+mUrl;
	
		var oHead = document.getElementsByTagName('HEAD').item(0); 
		var oDOM = document.createElement( "link" ); 
			oDOM.rel = "stylesheet"; 
			oDOM.type = "text/css"; 
			oDOM.href = fUrl; 
		oHead.appendChild( oDOM ); 
		this.loadedCSS[QName]=mUrl;
		return true;
	}, 
	 
	/**	@inner **/
	loadJS : function(QName) { 
		if( QName.length<1 || this.loadedJS[QName] ) return false;
		var mUrl = QName.replace(/[\.]/g,"/")+".js";
		var fUrl = this.getContextPath()+mUrl;
		//can't use post, otherwise http 412 error;
		var objReturn = doHttpPost(fUrl,null,"GET");
	    if(!objReturn.text)return false;
	    // jsLoader use     document.writeln("<scri"+"pt src='"+url+"' type='text/javascript'></sc"+"ript>");	    //but it can't ensure the js loaded complete order.	    // from dojo, seems can't run.
	    /**
	    var _prefix = "(function(){";
		var _suffix = "})()";
	    //1.eval(objReturn.text);
		//2.objectEval(window,_prefix + objReturn.text + _suffix);
		//3.objectEval(window,objReturn.text);
	    */
	    //*
	 	var oHead = document.getElementsByTagName('HEAD').item(0); 
		var oDOM = document.createElement( "script" ); 
			oDOM.language = "javascript"; 
			oDOM.type = "text/javascript"; 
			//oDOM.id = sId; 
			//oDOM.defer = true; 
			oDOM.text =  objReturn.text; 
			//can't use oDOM.src =  fUrl
		oHead.appendChild( oDOM );
		//alert(oDOM.outerHTML)
		//*/  
		this.loadedJS[QName]=mUrl;
		return true;
	},
	
	renderPath:	function(srcPath){
		var path = srcPath;
		var sEnd=path.charAt(path.length-1);
	    if(sEnd!="\\" && sEnd!="/"){
	    	path=path+"/";
	    }
		return path;
	}

}

$classLoader = new $ClassLoader();

function $import(names, type){
	$classLoader.load(names, type?type:"js");
}
function $importJS(names){ $import(names, "js");}
function $importCSS(names){$import(names, "css");}

